package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.UserDaoImpl;
import models.User;

public class RegisterServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //获取js页面传过来的参数
        String name = request.getParameter("userSignName");
        String pwd = request.getParameter("userSignPasswd");
        String pwd2 = request.getParameter("userSignPasswd2");

        //创建session
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();



        if (pwd.equals(pwd2)) {//验证两次密码是否一致
            System.out.println("密码相同 \n");

            User user = new User(); //实例化一个对象，组装属性
            user.setName(name);
            user.setPwd(pwd);
            user.setData(getSignTime());
            // System.out.println("222 " +user.getData());

            UserDaoImpl ud = new UserDaoImpl();

            try {
                if (ud.checkUserInfo(user)) {        //输入密码一致时 ，检测用户是否存在
                    System.out.println(" name exit,don't SIGN,please LOGIN");
                    //status为3 代表用户名已存在，不需要注册，直接登录
                    session.setAttribute("status","3");
                    out.write((String) session.getAttribute("status"));
                    // response.sendRedirect("/views/init.jsp");  //存在跳转到错误界面
                } else {
                    //密码一致，用户名不存在则存入数据库，并提示用户注册成功。
                    if (ud.save(user)) {
                        System.out.println("insert success");
                        //status为4  代表用户注册成功
                        System.out.println(" sign success");

                        session.setAttribute("status","4");
                        out.write((String) session.getAttribute("status"));
                        // response.sendRedirect("/views/init.jsp");   //注册成功，跳转到登录界面
                    } else {
                        System.out.println("insert failed");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } else {
            System.out.println("密码不一致");
            //status为2 表示密码不一致
            session.setAttribute("status","2");
            out.write((String) session.getAttribute("status"));
        }
        out.flush();
    }

    /**
     * 获取当前时间，转换格式
     * @return
     */
    public String getSignTime() {
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(df.format(day));
        String time = df.format(day);
        // System.out.println("111 " + time);
        return time;
    }
}